package cn.zians.controller;

import cn.zians.domain.Employee;
import cn.zians.service.IEmployeeService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 员工控制器
 *
 * @Date 2020.8.27 10:48
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有员工信息接口
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll")
    public String findAll(Model model) throws Exception {
        JSONObject jsonObject = new JSONObject();
        System.out.println("查询所有员工");
        try {
            List<Employee> employeeList = employeeService.findAll();
            model.addAttribute("list", "{ \"code\":0,\"message\":\"成功\",\"data\":" + JSONObject.toJSONString(employeeList) + "}");
        } catch (Exception e) {
            model.addAttribute("list", "{ \"error\":1,\"message\":\"失败\"}");
        }
        return "list";
    }

    /**
     * 添加员工信息接口
     *
     * @param employee
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public String addEmloyee(@Validated Employee employee, Model model) throws Exception {
        try {
            employeeService.addEmloyee(employee);
            model.addAttribute("add", "{ \"success\":0,\"message\":\"成功\"}");
        } catch (Exception e) {
            model.addAttribute("add", "{ \"error\":1,\"message\":\"失败\"}");
        }


        return "list";
    }

    /**
     * 删除员工信息接口
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteEmployee", method = RequestMethod.POST)
    public String deleteEmloyeeById(Integer id, Model model) throws Exception {
        try {
            employeeService.deleteEmloyee(id);
            model.addAttribute("delete", "{ \"success\":0,\"message\":\"成功\"}");
        } catch (Exception e) {
            model.addAttribute("delete", "{ \"error\":1,\"message\":\"失败\"}");
        }
        return "list";
    }

    /**
     * 更新员工信息接口
     *
     * @param employee
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateEmployee", method = {RequestMethod.POST})
    public String updateEmloyeeByid(@Validated Employee employee, Model model) throws Exception {
        try {
            employeeService.updateEmloyee(employee);
            model.addAttribute("update", "{ \"success\":0,\"message\":\"成功\"}");
        } catch (Exception e) {
            model.addAttribute("update", "{ \"error\":1,\"message\":\"失败\"}");
        }
        return "list";
    }
}
